<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Log as logModel;
use think\facade\Db;
use think\facade\View;

class Log extends Base{
    //用分页读取数据
    public function index(){
    	$keyword = input('keyword'); $order = ['id'=>'desc'];
		$rs = logModel::index($this->lang,$keyword,$order);

		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());
		
        return view();
    }

    /*删除指定日期的数据
    * 返回json值：success成功，error失败，date:error日期错误
    */
    public function delete_diy(){
        $fromdate = input('fromdate'); $todate = input('todate');
        if(empty($fromdate) or empty($todate) or strtotime($todate) < strtotime($fromdate)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => '']);
        }
        
        try {
            // 执行删除
            $dateWhere[] = ['endtime','between time',[$fromdate, $todate]];
            Db::name('log')->where($dateWhere)->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => '']);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
	
	/*单条删除
    * 返回json值：success成功，error失败
    */
    public function delete_one(){
        try {
            $id = input('id');
            $log = logModel::find($id);
            $log->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
	
    /*批量删除
    * 返回json值：success成功，error失败，select:error没有选择数据
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }

        try {
            Db::name('log')->delete($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}